<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">

      <el-form-item label="用户名" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入用户名"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="班级" prop="name">

        <el-select v-model="queryParams.classId" placeholder="请选择班级">
          <el-option value="" label="请选择班级"></el-option>
          <el-option
            v-for="item in classIds"
            :key="item.id"
            :label="item.name"
            :value="item.id">
          </el-option>
        </el-select>

      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>
    <el-row :gutter="10" class="mb8">

      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['user:user:edit']"
        >修改
        </el-button>

      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['user:user:remove']"
        >删除
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['user:user:export']"
        >导出
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          v-if="!multiple"
          @click="openClass"
          v-hasPermi="['user:user:export']"
        >分班
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="userList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column label="头像" align="center">
        <template slot-scope="scope">
          <div class="demo-image">
            <div class="block">
              <el-image
                style="width: 50px; height: 50px;border-radius: 50%;"
                :src="parseImg(scope.row.user.headImg)"
                fit="cover"></el-image>
            </div>
          </div>
        </template>
      </el-table-column>
      <el-table-column label="手机号" align="center" prop="user.phone"/>
      <el-table-column label="姓名" align="center" prop="user.name"/>
      <el-table-column label="年龄" align="center" prop="user.age"/>
      <el-table-column label="班级" align="center" prop="className"/>
      <el-table-column label="性别" align="center" prop="user.sex">

        <template slot-scope="scope">
          {{ scope.row.user.sex == 0 ?"男":"女"}}
        </template>
      </el-table-column>
      <el-table-column label="设备绑定状态" align="center">
        <template slot-scope="scope">
          {{ scope.row.user.isEquipment == 0 ?"未绑定":scope.row.user.isEquipment == 1 ? "绑定中":scope.row.equipmentId}}
        </template>

      </el-table-column>

      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">

          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['user:user:remove']"
          >删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.page"
      :limit.sync="queryParams.size"
      @pagination="getList"
    />

    <!-- 添加或修改用户信息对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="手机号" prop="phone">
          <el-input v-model="form.phone" placeholder="请输入手机号"/>
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input v-model="form.password" placeholder="请输入密码"/>
        </el-form-item>
        <el-form-item label="用户名" prop="name">
          <el-input v-model="form.name" placeholder="请输入用户名"/>
        </el-form-item>
        <el-form-item label="年龄" prop="age">
          <el-input v-model="form.age" placeholder="请输入年龄"/>
        </el-form-item>
        <el-form-item label="性别" prop="sex">
          <el-select v-model="form.sex" placeholder="请选择性别">
            <el-option label="请选择字典生成" value=""/>
          </el-select>
        </el-form-item>
        <el-form-item label="头像" prop="headimg">
          <el-input v-model="form.headimg" placeholder="请输入头像"/>
        </el-form-item>
        <el-form-item label="设备绑定状态" prop="isequipment">
          <el-input v-model="form.isequipment" placeholder="请输入设备绑定状态"/>
        </el-form-item>
        <el-form-item label="权限" prop="auth">
          <el-input v-model="form.auth" placeholder="请输入权限"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <el-dialog title="分班" :visible.sync="openclass" width="500px" append-to-body>
      <el-form ref="form" :model="user_class" label-width="80px">
        <el-form-item label="班级" prop="classId">

          <el-select v-model="user_class.classId" placeholder="请选择班级">
            <el-option value="" label="请选择班级"></el-option>
            <el-option
              v-for="item in classIds"
              :key="item.id"
              :label="item.name"
              :value="item.id">
            </el-option>
          </el-select>

        </el-form-item>

      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="division">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
  import {
    listUser,
    getUser,
    delUser,
    addUser,
    updateUser,
    exportUser,
    listClassId,
    UpdateUserClass
  } from "@/api/baby/user";


  export default {
    name: "User",
    components: {},
    data() {
      return {
        // 遮罩层
        loading: true,

        // 选中数组
        ids: [],
        // 非单个禁用
        single: true,
        // 非多个禁用
        multiple: true,
        // 显示搜索条件
        showSearch: true,
        user_class: {classId: null},
        // 总条数
        total: 0,
        // 用户信息表格数据
        userList: [],
        // 弹出层标题
        title: "",
        // 是否显示弹出层
        open: false,
        openclass: false,
        classIds: [],
        // 查询参数
        queryParams: {
          page: 1,
          size: 10,
          name: null,
          classId: null,

        },
        // 表单参数
        form: {},
        // 表单校验
        rules: {
          phone: [
            {required: true, message: "手机号不能为空", trigger: "blur"}
          ],
          sex: [
            {required: true, message: "性别不能为空", trigger: "change"}
          ],
          isequipment: [
            {required: true, message: "设备绑定状态不能为空", trigger: "blur"}
          ],

        }
      };
    },
    created() {
      this.getList();
      this.getClassID();
    },
    methods: {
      //分班
      openClass() {
        this.openclass = true;

      },
      division() {


        var data = {ids: this.ids, classId: this.user_class.classId}

        console.log(data)
        UpdateUserClass(data).then(res => {
          this.openclass = false;
          this.msgSuccess("修改成功");
          this.getList();
        })

      },

      /** 查询用户信息列表 */
      getList() {
        this.loading = true;
        listUser(this.queryParams).then(response => {
          this.userList = response.data;
          this.total = response.count;
          this.loading = false;
        });
      },

      getClassID() {
        listClassId().then(res => {
          this.classIds = res.data;
        })
      },

      parseImg(url) {
        return "http://39.106.123.27:81/" + url;
      },
      // 取消按钮
      cancel() {
        this.open = false;
        this.openclass = false;
        this.reset();
      },
      // 表单重置
      reset() {
        this.form = {
          id: null,
          phone: null,
          password: null,
          name: null,
          age: null,
          sex: null,
          headimg: null,
          isequipment: null,
          auth: null
        };
        this.resetForm("form");
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.pageNum = 1;
        this.getList();
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.resetForm("queryForm");
        this.handleQuery();
      },
      // 多选框选中数据
      handleSelectionChange(selection) {
        this.ids = selection.map(item => item.user.id)
        this.single = selection.length !== 1
        this.multiple = !selection.length
      },


      /** 新增按钮操作 */
      handleAdd() {
        this.reset();
        this.open = true;
        this.title = "添加用户信息";
      },
      /** 修改按钮操作 */
      handleUpdate(row) {
        this.reset();
        const id = row.id || this.ids
        getUser(id).then(response => {
          this.form = response.data;
          this.open = true;
          this.title = "修改用户信息";
        });
      },
      /** 提交按钮 */
      submitForm() {
        this.$refs["form"].validate(valid => {
          if (valid) {
            if (this.form.id != null) {
              updateUser(this.form).then(response => {
                this.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              });
            } else {
              addUser(this.form).then(response => {
                this.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              });
            }
          }
        });
      },
      /** 删除按钮操作 */
      handleDelete(row) {
        const ids = row.id || this.ids;
        this.$confirm('是否确认删除用户信息编号为"' + ids + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function () {
          return delUser(ids);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        })
      },
      /** 导出按钮操作 */
      handleExport() {
        const queryParams = this.queryParams;
        this.$confirm('是否确认导出所有用户信息数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function () {
          return exportUser(queryParams);
        }).then(response => {
          this.download(response.msg);
        })
      }
    }
  };
</script>
